package canss.webservice.controls;


import org.apache.beehive.controls.api.bean.Control;
import org.apache.beehive.controls.api.bean.ControlImplementation;

import canss.encoder.DBStringEncoder;
import canss.webservice.model.EventDescription;


import weblogic.jdbc.SQLExceptionWrapper;

import java.io.Serializable;

@ControlImplementation
public class EventDescriptionControlImpl implements EventDescriptionControl,
		Serializable
{
	private static final long serialVersionUID = -8198839875510765561L;

	@Control
	private BusinessSystemDB businesssystemDB;

	public Boolean isDuplicatedEventID(Integer _eventid)
			throws SQLExceptionWrapper
	{
		return businesssystemDB.getNumofEventDescByEventID(_eventid) > 0;
	}

	public EventDescription getEventDescByEventID(Integer _eventid)
			throws SQLExceptionWrapper
	{
		EventDescription[] ret = businesssystemDB
				.getEventDescByEventID(_eventid);
		if (ret == null || ret.length == 0)
			return null;

		ret[0].setEVENTNAME(DBStringEncoder.DB2Server(ret[0].getEVENTNAME()));
		ret[0].setONELINEDESC(DBStringEncoder
				.DB2Server(ret[0].getONELINEDESC()));

		return ret[0];
	}

	public int addEventDesc(EventDescription _eventdesc)
			throws SQLExceptionWrapper
	{
		int ret = businesssystemDB.addNewEventDesc(_eventdesc.getEVENTID(), _eventdesc.getLEVEL(),
				DBStringEncoder.Server2DB(_eventdesc.getEVENTNAME()), DBStringEncoder.Server2DB(_eventdesc.getONELINEDESC()));
		return ret;
	}

	public int removeEventDesc(Integer _eventid) throws SQLExceptionWrapper
	{
		return businesssystemDB.removeEventDescByEventID(_eventid);
	}

	public EventDescription[] getEntireEventDesc() throws SQLExceptionWrapper
	{
		EventDescription[] ret = businesssystemDB.getEntireEventDesc();
		if (ret == null || ret.length == 0)
			return null;
		for (EventDescription eventdesc : ret)
		{
			eventdesc.setEVENTNAME(DBStringEncoder.DB2Server(eventdesc
					.getEVENTNAME()));
			eventdesc.setONELINEDESC(DBStringEncoder.DB2Server(eventdesc
					.getONELINEDESC()));
		}
		return ret;
	}
}